//
//  ActViewController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/5/30.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "ActViewController.h"
#import "activityViewCell.h"

#import "ActViewListsModel.h"

//积分活动

#import "EasyToTescoNewsDetailsVc.h"
#import "RecommendedController.h"
#import "ShopDetailsController.h"


@interface ActViewController ()
{
    UIImageView *noDrawImage;
}

@property(nonatomic,strong)NSMutableArray *actListsArray;

@end

@implementation ActViewController

static NSString *const activityViewCellIdentifier = @"activityViewCellIdentifier";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getActivityListsData];
}


- (void)getActivityListsData
{
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:nil AndMenthodName:@"/api/Activity/lists" completeBlock:^(id object) {
        if (JudgeBackDataCode) {
         
            NSArray *dataArr = [object objectForKey:@"data"];
            for (NSDictionary *tempDict in dataArr) {
                ActViewListsModel *actViewListsModel = [[ActViewListsModel alloc] initWithDictionary:tempDict error:nil];
                if (actViewListsModel) {
                    [self.actListsArray addObject:actViewListsModel];
//                    NSLog(@"%@",self.actListsArray);
                }
            }
        }else{
            
            //暂无地址(图)
            noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noActist"]];
               [self.view addSubview:noDrawImage];

                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/10);
                   make.width.mas_equalTo(WIDTH/2);
                    make.height.mas_equalTo(HEIGHT/4);
            }];
        }
        
        [self.tableView reloadData];
    }];
}

#pragma mark>>>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.actListsArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:activityViewCellIdentifier];
    if (cell==nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:activityViewCellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    CGFloat height;
    if (_homePush) {
        height = (HEIGHT-64-HEIGHT/8)/3;
    }else{
        height = (HEIGHT-64-49-HEIGHT/8)/3;
    }
    
    ActViewListsModel *actViewListsModel = self.actListsArray[indexPath.section];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,WIDTH,height)];
    [image sd_setImageWithURL:[NSURL URLWithString:actViewListsModel.pic] placeholderImage:[UIImage imageNamed:@"goodsLoadJGG"]];
    
    [cell.contentView addSubview:image];
    return cell;
    
}


#pragma mark代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActViewListsModel *actViewListsModel = self.actListsArray[indexPath.section];
    NSInteger typeIndex = [actViewListsModel.type integerValue];
    [self swichActivityType:typeIndex  indexPath:indexPath];
    
}

- (void)swichActivityType:(NSInteger)type indexPath:(NSIndexPath *)indexPath
{
    ActViewListsModel *actViewListsModel = self.actListsArray[indexPath.section];
    switch (type) {
        case 1:
            //商品
        {
            
            ShopDetailsController *detailsVC = [[ShopDetailsController alloc] init];
            detailsVC.goodsId = actViewListsModel.product_id;
            [self.navigationController pushForm:self.navigationController pushToAndHidden:detailsVC];
            break;

        }
            
        case 2:
            //商品列表
        {
            
            RecommendedController*recommendedVC = [[RecommendedController alloc] init];
            recommendedVC.title = actViewListsModel.title;
            recommendedVC.is_actPush = YES;
            recommendedVC.product_id = actViewListsModel.product_id;
            [self.navigationController pushForm:self.navigationController pushToAndHidden:recommendedVC];
            
            break;
        }
            
        case 3:
            //文章
        {
            EasyToTescoNewsDetailsVc *newsDetailsVC = [[EasyToTescoNewsDetailsVc alloc] init];
            newsDetailsVC.is_actPush = YES;
            newsDetailsVC.link = actViewListsModel.url;
            newsDetailsVC.title = actViewListsModel.title;
            [self.navigationController pushForm:self.navigationController pushToAndHidden:newsDetailsVC];
            break;
        }
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_homePush) {
        return (HEIGHT-64-HEIGHT/8)/3;
    }else{
        return (HEIGHT-64-49-HEIGHT/8)/3;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (NSMutableArray *)actListsArray
{
    if (!_actListsArray) {
        _actListsArray = [NSMutableArray array];
    }
    return _actListsArray;
}

@end
